const db = require("../db")

// 查看学生成绩
exports.tsscore = (req, res) => {
  const sql = `select * from sc5342.teacher_score_view where t_no=${req.body.tno} order by c_type desc, c_no, class_no, s_no;`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    results.forEach((e, i) => {
      results[i].c_type = e.c_type ? '必修' : '选修'
    })
    res.cc(results, 200)
  })
}

// 修改成绩处理函数
exports.updatescore = (req, res) => {
  const userinfo = req.body
  const sql = `update sc5342.student_course5342 set score=${userinfo.new} where (t_no, c_no, s_no) in (select * from (select t_no, c_no, s_no from sc5342.teacher_score_view where c_title='${userinfo.ctitle}' and t_no='${userinfo.tno}' and s_no='${userinfo.sno}') as t);`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    if (results.affectedRows) return res.cc({msg: '修改成功!'}, 200)
    res.cc({msg: '修改失败!'})
  })
}

// 查看下学期课程
exports.tscourse = (req, res) => {
  const sql = `select * from sc5342.teacher_course_view where t_no=${req.body.tno} order by c_type desc, c_no, class_no;`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    let i = 0
    let repeat = false
    while (i<results.length) {
      repeat = false
      results[i].c_type= results[i].c_type ? '必修' : '选修'
      for (let k=0; k<i; k++) {
        if (results[i].t_no == results[k].t_no && results[i].c_no == results[k].c_no) {
          results[k].class_no += ', ' +  results[i].class_no
          results[k].count +=  results[i].count
          results.splice(i,1)
          repeat = true
          break
        }
      }
      if (!repeat) i++;
    }
    res.cc(results, 200)
  })
}

// 发布选修课程处理函数
exports.optionalcourse = (req, res) => {
  const userinfo = req.body
  const nature = `Optionalcourse('${userinfo.tno}', '${userinfo.ctitle}')`
  const sql = `select ${nature};`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    if (results[0][nature]) return res.cc({msg: '修改成功!'}, 200)
    res.cc({msg: '修改失败!'})
  })
}

// 发布必修课程处理函数
exports.requiredcourse = (req, res) => {
  const userinfo = req.body
  const nature = `Requiredcourse('${userinfo.tno}', '${userinfo.ctitle}', '${userinfo.classno}')`
  const sql = `select ${nature};`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    if (results[0][nature]) return res.cc({msg: '修改成功!'}, 200)
    res.cc({msg: '修改失败!'})
  })
}

// 撤回课程处理函数
exports.tccourse = (req, res) => {
  const userinfo = req.body
  const sql = `delete from sc5342.teacher_course5342 where t_no=${userinfo.tno} and c_no=${userinfo.cno};`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    if (results.affectedRows) return res.cc({msg: '删除成功!'}, 200)
    res.cc({msg: '删除失败!'})
  })
}

// 查看班级班长
exports.selectmonitor = (req, res) => {
  const sql = `select * from sc5342.class_view;`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    res.cc(results, 200)
  })
}

// 任命班长
exports.appointmonitor = (req, res) => {
  const userinfo = req.body
  const nature = `Appointheader('${userinfo.tno}', '${userinfo.classno}', '${userinfo.sno}')`
  const sql = `select ${nature};`
  db.query(sql, function (err, results) {
    if (err) return res.cc({msg: err.sqlMessage})
    if (results[0][nature]) return res.cc({msg: '修改成功!'}, 200)
    res.cc({msg: '修改失败!'})
  })
}